package com.proactive.webapp.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.DataSource;
import com.proactive.webapp.dao.ManagerDAO;
import com.proactive.webapp.entity.Manager;

public class ManagerDAOImpl implements ManagerDAO {
	
	private DataSource dataSource;
	private Connection conn = null ;
	private ResultSet rs = null ;
	private PreparedStatement smt = null ;
	
	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}
	
	public Manager checkLogin(Manager manager){
		String sql = "SELECT * FROM manager WHERE managerAccount=? AND managerPassword=?";
		manager.setLogin(false);
		try {
			conn = dataSource.getConnection();
			smt = conn.prepareStatement(sql);
			smt.setString(1, manager.getAccount());
			smt.setString(2, manager.getPassword());
			rs = smt.executeQuery();
			if(rs.next()){
				manager.setName(rs.getString("managerName"));
				manager.setLogin(true);
			}
			rs.close();
			smt.close();
 
		} catch (SQLException e) {
			throw new RuntimeException(e);
 
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {}
			}
		}
		return manager;
	}
}
